package org.jawa.menu;

import org.jawa.core.cache.CacheSizes;
import org.jawa.core.cache.Cacheable;
import org.jawa.core.cache.CannotCalculateSizeException;
import org.jawa.core.exception.NotFoundException;

/**
 * 
 * 系统菜单.
 * 
 * @author Administrator
 *
 */
public class Menu implements Cacheable {

	private static final long serialVersionUID = 205683435717749326L;

	private Long menuId;
	private String menuName;
	private Long menuParentId;
	private String menuPicture;
	private String menuUrl;
	private Long menuSort;
	private String menuClick;
	private String menuDbClick;
	private String moduleDetail;
	private String remark;

	public Long getMenuId() {
		return menuId;
	}

	public String getMenuName() {
		return menuName;
	}

	public Long getMenuParentId() {
		return menuParentId;
	}

	public String getMenuPicture() {
		return menuPicture;
	}

	public String getMenuUrl() {
		return menuUrl;
	}

	public Long getMenuSort() {
		return menuSort;
	}

	public String getMenuClick() {
		return menuClick;
	}

	public String getMenuDbClick() {
		return menuDbClick;
	}

	public String getModuleDetail() {
		return moduleDetail;
	}

	public String getRemark() {
		return remark;
	}

	public void setMenuId(Long menuId) {
		this.menuId = menuId;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public void setMenuParentId(Long menuParentId) {
		this.menuParentId = menuParentId;
	}

	public void setMenuPicture(String menuPicture) {
		this.menuPicture = menuPicture;
	}

	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}

	public void setMenuSort(Long menuSort) {
		this.menuSort = menuSort;
	}

	public void setMenuClick(String menuClick) {
		this.menuClick = menuClick;
	}

	public void setMenuDbClick(String menuDbClick) {
		this.menuDbClick = menuDbClick;
	}

	public void setModuleDetail(String moduleDetail) {
		this.moduleDetail = moduleDetail;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public int getCachedSize() throws CannotCalculateSizeException {
		int size = 0;
		size += CacheSizes.sizeOfLong();
		size += CacheSizes.sizeOfString(menuName);
		size += CacheSizes.sizeOfLong();
		size += CacheSizes.sizeOfString(menuPicture);
		size += CacheSizes.sizeOfString(menuUrl);
		size += CacheSizes.sizeOfLong();
		size += CacheSizes.sizeOfString(menuClick);
		size += CacheSizes.sizeOfString(menuDbClick);
		size += CacheSizes.sizeOfString(moduleDetail);
		size += CacheSizes.sizeOfString(remark);
		return size;
	}

	@Override
	public String toString() {
		return "Menu [menuClick=" + menuClick + ", menuDbClick=" + menuDbClick + ", menuId=" + menuId + ", menuName="
				+ menuName + ", menuParent=" + menuParentId + ", menuPicture=" + menuPicture + ", menuSort=" + menuSort
				+ ", menuUrl=" + menuUrl + ", moduleDetail=" + moduleDetail + ", remark=" + remark + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((menuClick == null) ? 0 : menuClick.hashCode());
		result = prime * result + ((menuDbClick == null) ? 0 : menuDbClick.hashCode());
		result = prime * result + ((menuId == null) ? 0 : menuId.hashCode());
		result = prime * result + ((menuName == null) ? 0 : menuName.hashCode());
		result = prime * result + ((menuParentId == null) ? 0 : menuParentId.hashCode());
		result = prime * result + ((menuPicture == null) ? 0 : menuPicture.hashCode());
		result = prime * result + ((menuSort == null) ? 0 : menuSort.hashCode());
		result = prime * result + ((menuUrl == null) ? 0 : menuUrl.hashCode());
		result = prime * result + ((moduleDetail == null) ? 0 : moduleDetail.hashCode());
		result = prime * result + ((remark == null) ? 0 : remark.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Menu other = (Menu) obj;
		if (menuClick == null) {
			if (other.menuClick != null)
				return false;
		} else if (!menuClick.equals(other.menuClick))
			return false;
		if (menuDbClick == null) {
			if (other.menuDbClick != null)
				return false;
		} else if (!menuDbClick.equals(other.menuDbClick))
			return false;
		if (menuId == null) {
			if (other.menuId != null)
				return false;
		} else if (!menuId.equals(other.menuId))
			return false;
		if (menuName == null) {
			if (other.menuName != null)
				return false;
		} else if (!menuName.equals(other.menuName))
			return false;
		if (menuParentId == null) {
			if (other.menuParentId != null)
				return false;
		} else if (!menuParentId.equals(other.menuParentId))
			return false;
		if (menuPicture == null) {
			if (other.menuPicture != null)
				return false;
		} else if (!menuPicture.equals(other.menuPicture))
			return false;
		if (menuSort == null) {
			if (other.menuSort != null)
				return false;
		} else if (!menuSort.equals(other.menuSort))
			return false;
		if (menuUrl == null) {
			if (other.menuUrl != null)
				return false;
		} else if (!menuUrl.equals(other.menuUrl))
			return false;
		if (moduleDetail == null) {
			if (other.moduleDetail != null)
				return false;
		} else if (!moduleDetail.equals(other.moduleDetail))
			return false;
		if (remark == null) {
			if (other.remark != null)
				return false;
		} else if (!remark.equals(other.remark))
			return false;
		return true;
	}

	/**
	 * 返回当前菜单的父菜单对象.
	 * @return
	 */
	public Menu getParent() {
		try {
			return MenuManager.getInstance().getMenu(menuParentId);
		} catch (NotFoundException e) {
			//忽略
		}
		return null;
	}

}
